//
//  SISSContractFillInPropertyCell.m
//  SISSAPP
//
//  Created by 优房网 on 2017/8/28.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSContractFillInPropertyCell.h"

@interface SISSContractFillInPropertyCell ()<UITextFieldDelegate>

/* leftLabel */
@property (weak, nonatomic) UILabel *leftLabel;
/// Label
@property (weak, nonatomic) UILabel *label;
/// textField
@property (weak, nonatomic) UITextField *textField;
/// rightTextField
@property (weak, nonatomic) UITextField *rightTextField;
/// midLabel
@property (weak, nonatomic) UILabel *midLabel;
/// unitLabel
@property (weak, nonatomic) UILabel *unitLabel;
/* bottomLine */
@property (weak, nonatomic) UIView *bottomLine;

@end
@implementation SISSContractFillInPropertyCell



- (void)setContractModel:(SISSContractModel *)contractModel {
    [super setContractModel:contractModel];
    
    
    self.textField.text = contractModel.rightStr;
    self.rightTextField.text = contractModel.rightTwoStr;
    
    if (!contractModel.leftStr) return;
    self.leftLabel.hidden = !contractModel.leftStr;
    if ([[contractModel.leftStr substringToIndex:1] isEqualToString:@"*"]) {
        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:contractModel.leftStr];
        [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,1)];
        self.leftLabel.attributedText = attributedStr;
    } else {
        self.leftLabel.text = contractModel.leftStr;
    }
    
    self.rightTextField.userInteractionEnabled = NO;
    self.label.text = @"签订后";
    self.textField.placeholder = @"几";
    self.midLabel.text = @"日内支付";
    self.unitLabel.text = @"元";
}


#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    if (textField == self.rightTextField) {
        self.contractModel.rightTwoStr = textField.text;
    } else if (textField == self.textField) {
        self.contractModel.rightStr = textField.text;
    }
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField == self.rightTextField) {
        NSString *str = [NSString stringWithFormat:@"%@%@",textField.text,string];
        if (str.length > 2) {
            [SISSTools showSVTitle:@"不超过2位数字" andTime:1 andColor:YES];
            return NO;
        }
    }
    return YES;
}

#pragma mark - UI
- (void)initUI {
    [super initUI];
    
    UILabel *leftLabel = [[UILabel alloc] init];
    leftLabel.textColor = MAIN_TEXT_COLOR;
    leftLabel.font = [UIFont systemFontOfSize:16];
    leftLabel.hidden = YES;
    [self.contentView addSubview:leftLabel];
    self.leftLabel = leftLabel;
    
    UILabel *label = [[UILabel alloc] init];
    label.numberOfLines = 0;
    label.textColor = MAIN_TEXT_COLOR;
    label.font = [UIFont systemFontOfSize:16];
    label.textAlignment = NSTextAlignmentRight;
    label.text = @"共有人为";
    [self.contentView addSubview:label];
    self.label = label;
    
    UITextField *textField = [[UITextField alloc] init];
    textField.textColor = MAIN_TEXT_COLOR;
    textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" " attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:SECONDARY_TEXT_COLOR}];
    textField.placeholder = @"姓名";
    textField.textAlignment = NSTextAlignmentCenter;
    textField.delegate = self;
    [self.contentView addSubview:textField];
    self.textField = textField;
    
    UILabel *midLabel = [[UILabel alloc] init];
    midLabel.numberOfLines = 0;
    midLabel.textColor = MAIN_TEXT_COLOR;
    midLabel.font = [UIFont systemFontOfSize:16];
    midLabel.text = @"，份额";
    [self.contentView addSubview:midLabel];
    self.midLabel = midLabel;
    
    UITextField *rightTextField = [[UITextField alloc] init];
    rightTextField.textColor = MAIN_TEXT_COLOR;
    rightTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" " attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:SECONDARY_TEXT_COLOR}];
    rightTextField.placeholder = @"几";
    rightTextField.delegate = self;
    rightTextField.textAlignment = NSTextAlignmentCenter;
    rightTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self.contentView addSubview:rightTextField];
    self.rightTextField = rightTextField;
    
    UILabel *unitLabel = [[UILabel alloc] init];
    unitLabel.numberOfLines = 0;
    unitLabel.textColor = MAIN_TEXT_COLOR;
    unitLabel.font = [UIFont systemFontOfSize:16];
    unitLabel.textAlignment = NSTextAlignmentRight;
    unitLabel.text = @"%";
    [self.contentView addSubview:unitLabel];
    self.unitLabel = unitLabel;
    
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:bottomLine];
    self.bottomLine = bottomLine;

}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    CGFloat margin = 8.0;
    
    [self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView).offset(1.5 * margin);
        make.left.equalTo(weakSelf.contentView).offset(2 * margin);
    }];
    
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.textField.mas_left).offset(-0.5 * margin);
        make.centerY.equalTo(weakSelf.contentView);
    }];
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.midLabel.mas_left).offset(-0.5 * margin);
        make.centerY.equalTo(weakSelf.contentView);
//        make.width.mas_equalTo(6 * margin);
    }];
    
    [self.midLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.rightTextField.mas_left).offset(-margin);
        make.centerY.equalTo(weakSelf.contentView);
    }];
    
    [self.rightTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.unitLabel.mas_left);
        make.centerY.equalTo(weakSelf.unitLabel);
//        make.width.mas_equalTo(3 * margin);
    }];
    
    [self.unitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView).offset(1.5 * margin);
        make.right.equalTo(weakSelf.contentView).offset(-margin);
    }];
    
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.unitLabel.mas_bottom).offset(1.5 * margin);
        make.left.right.bottom.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1);
    }];
}


@end
